<?php

/*****************************************************************************
 * Copyright (C) 2009 Gwathuirim
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Public License for more details
 *
 * To obtain a copy of the GNU General Public License, please see
 * <http://www.gnu.org.licenses/>
 *****************************************************************************/
 
require_once('class.template.php');
require_once('phpmailer/class.phpmailer.php'); 

abstract class ReportMail {

	var $environmentVars;

	public function __construct($environmentVars) {
		$this->environmentVars = $environmentVars;
	}

	/**
	 * Sends this reportmail as mail.
	 */
	abstract public function send($recipient);
	
	/**
	 * Creates and setups a Template
	 */
	public function createTemplate($filename) {
		$resultTpl = & new Template("templates/".$filename);
		$resultTpl->set('reportMail', $this);
		
		//Pass in the environmentVars
		foreach ($this->environmentVars as $environmentVarsKey => $environmentVarsValue) {
			$resultTpl->set($environmentVarsKey, $environmentVarsValue);
		}
	
		return $resultTpl;
	}
	
	/**
	 * Creates a mailer and returns it
	 */ 
	protected function setupMail($recipient) {
		$mailer = new PHPMailer();
		$mailer->IsSMTP(); // send via SMTP
		$mailer->SMTPAuth = true; // turn on SMTP authentication
		$mailer->Host = SMTP_HOST;
		$mailer->Port = SMTP_PORT;
		$mailer->Username = SMTP_USER; // SMTP username
		$mailer->Password = SMTP_PASSWORD; // SMTP password
		$mailer->From = SMTP_USER;
		$mailer->FromName = SMTP_USER;
		$mailer->AddReplyTo(SMTP_USER); //TODO: REMOVE
		$mailer->IsHTML(true); // send as HTML
		$mailer->AddAddress($recipient);
		$mailer->CharSet = 'UTF-8';
		
		return $mailer;
	}
}
 
?>